package com.ubird.particle;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.Random;

public class ParticleNode {

    private int initLife;
    private int life;

    private int initX;
    private int x;
    private int initY;
    private int y;
    private int width;
    private int height;

    private float angle;
    private float vr;

    private float vx;
    private float vy;
    private float ax;
    private float ay;
    private float initVy;
    private float initVx;
    private Image texture;

    private Composite[] alaphaComposite = new Composite[] { AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f),
            AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f) };
    private int type;
    private int initDelay;
    private int delay;

    private static Random random = new Random();

    public final static int TYPE_ONCE = 0;
    public final static int TYPE_CYCLE = 1;
    public final static int TYPE_ONCE_AND_NOT_DISPLAY_FIRST = 2;

    public ParticleNode(int x, int y, int width, int height, float vx, float vy, float vr, float ax, float ay, int life,
            int delay, int type) {
        this.x = 0;
        this.y = 0;
        this.initX = x;
        this.initY = y;
        this.width = width;
        this.height = height;
        this.vr = vr;
        this.vx = vx;
        this.vy = vy;
        this.initVx = vx;
        this.initVy = vy;
        this.ax = ax;
        this.ay = ay;
        this.life = type == TYPE_ONCE_AND_NOT_DISPLAY_FIRST ? 0 : life;
        this.initLife = life;
        this.type = type;
        this.delay = delay;
        this.initDelay = delay;
    }

    public void draw(Graphics g) {
        if (!isNeedDraw()) {
            return;
        }
        Graphics2D g2d = (Graphics2D) g.create();
        // Color color = new Color(255*(initLife-life)/initLife, 255, 0);
        g2d.setComposite(alaphaComposite[(initLife - life) * (alaphaComposite.length - 1) / initLife]);
        if (texture != null) {
            // g2d.setTransform(AffineTransform.getRotateInstance(20));
            // g2d.setTransform(AffineTransform.getTranslateInstance(x+initX,
            // y+initY));
            g2d.rotate(angle, x + initX + width / 2, y + initY + height / 2);
            // g2d.drawImage(texture, x+initX, y+initY, x+initX+width,
            // y+initY+height, 10, 354, 100, 434, null);
            g2d.drawImage(texture, x + initX, y + initY, x + initX + width, y + initY + height, 13, 230, 93, 305, null);
        } else {
            Color color = new Color(125 + 125 * (life) / initLife, 255 * (initLife - life) / initLife, 0);
            g2d.setColor(color);
            g2d.rotate(angle, x + initX + width / 2, y + initY + height / 2);
            g2d.fillOval(x + initX, y + initY, width, height);
        }

        g2d.dispose();
    }

    public void setTexture(Image texture) {
        this.texture = texture;
    }

    public void update(int time) {
        if (!isNeedUpdate()) {
            return;
        }

        if (this.delay > 0) {
            this.delay--;
            return;
        }
        life--;
        vx += ax;
        vy += ay;
        angle += vr;
        x += vx;
        y += vy;
        if (life == 0 && type == TYPE_CYCLE)
            init(initX, initY);
    }

    private boolean isNeedDraw() {
        return this.delay <= 0 && this.life > 0;
    }

    private boolean isNeedUpdate() {
        return this.life > 0 || life == 0 && type != TYPE_ONCE;
    }

    public void update() {
        update(0);
    }

    public void init(int x, int y) {
        this.x = 0;
        this.y = 0;
        this.initX = x;
        this.initY = y;
        this.vx = initVx;
        this.vy = initVy;
        this.life = initLife;
        this.delay = initDelay;
    }

    public void initY(int y) {
        init(initX, y);
    }

    enum PathCalculator {
        YunJiaSu() {
            @Override
            public float calX(float vx0, float ax, int time) {
                return vx0 + ax * time / 80f;
            }

            @Override
            public float calY(float vy0, float ay, int time) {
                return vy0 + ay * time / 80f;
            }
        };

        public abstract float calX(float vx0, float vxa, int time);

        public abstract float calY(float vy0, float vya, int time);
    }
}